//
//  UIColor+Palette.swift
//  DesignKit
//
//  Created by Jake Lin on 20/10/20.
//
// The colors are picked up from https://backpack.github.io/guidelines/colors

import UIKit

public extension UIColor {
  static let designKit = DesignKitPalette.self

  enum DesignKitPalette {

    public static let accent: UIColor = UIColor(hex: 0xFF6707)

    public static let primary: UIColor = dynamicColor(light: UIColor(hex: 0xFFC807), dark: UIColor(hex: 0x6d9feb))

    public static let background: UIColor = dynamicColor(light: .white, dark: .black)

    public static let secondaryBackground: UIColor = dynamicColor(light: UIColor(hex: 0xFAFAFA), dark: UIColor(hex: 0x1D1B20))

    public static let tertiaryBackground: UIColor = dynamicColor(light: .black.withAlphaComponent(0.1), dark: UIColor(hex: 0x2C2C2E))

    public static let line: UIColor = dynamicColor(light: UIColor(hex: 0xfafafa), dark: UIColor(hex: 0xfafafa))

    public static let primaryText: UIColor = dynamicColor(light: UIColor(hex: 0x262625), dark: .white)

    public static let secondaryText: UIColor = dynamicColor(light: UIColor(hex: 0x605F5B), dark: UIColor(hex: 0x605F5B))

    public static let tertiaryText: UIColor = dynamicColor(light: UIColor(hex: 0x9A9891), dark: UIColor(hex: 0x9A9891))

    public static let quaternaryText: UIColor = dynamicColor(light: UIColor(hex: 0xb2b2bf), dark: UIColor(hex: 0x8E8E93))

    static private func dynamicColor(light: UIColor, dark: UIColor) -> UIColor {
      if #available(iOS 13.0, *) {
        return UIColor { $0.userInterfaceStyle == .dark ? dark : light }
      } else {
        return light
      }
    }
  }
}

public extension UIColor {
  convenience init(hex: Int) {
    let components = (
      R: CGFloat((hex >> 16) & 0xff) / 255,
      G: CGFloat((hex >> 08) & 0xff) / 255,
      B: CGFloat((hex >> 00) & 0xff) / 255
    )
    self.init(red: components.R, green: components.G, blue: components.B, alpha: 1)
  }
}
